package ai.plantdata.Behavior.mode.ObserverPattern.classes;

import ai.plantdata.Behavior.mode.ObserverPattern.abstractClasses.Observer;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: WangHui
 * @Date: 2020/12/23 15:14
 * @Description: 发布者
 */
public class Subject {
    private List<Observer> observers = new ArrayList<Observer>();
    private int state;

    public int getState(){
        return state;
    }

    public void setState(int state){
        this.state = state;
        notifyAllObservers();
    }

    /**
     * 增加一个接受观察着
     * @param observer
     */
    public void attach(Observer observer){
        observers.add(observer);
    }

    /**
     * 通知所有的观察者
     */
    public void notifyAllObservers(){
        for (Observer observer : observers) {
            observer.update();
        }
    }
}
